<template>
  <div id="examinationView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="title" label="名称" style="min-width: 240px">
        <a-input v-model="searchParams.title" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item field="tags" label="标签" style="min-width: 240px">
        <a-input-tag v-model="searchParams.tags" placeholder="请输入标签" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSubmit">提交</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      :ref="tableRef"
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD HH:mm:ss") }}
      </template>
      <template #startTime="{ record }">
        {{ moment(record.startTime).format("YYYY-MM-DD HH:mm:ss") }}
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button
            type="primary"
            @click="toExamQuestion(record)"
            :disabled="!isExamAvailable(record.startTime)"
          >
            进入考试
          </a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment/moment";
import {
  Exam,
  ExamControllerService,
  ExamQueryRequest,
} from "../../../generated";

const tableRef = ref();

const dataList = ref([]);
const total = ref(0);
const searchParams = ref<ExamQueryRequest>({
  pageSize: 8,
  current: 1,
});

const loadData = async () => {
  const res = await ExamControllerService.listExamByPageUsingPost(
    searchParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败" + res.message);
  }
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

const columns = [
  {
    title: "考场号",
    dataIndex: "id",
  },
  {
    title: "考场名",
    dataIndex: "examName",
  },
  {
    title: "描述",
    dataIndex: "examDesc",
  },
  {
    title: "创建者",
    dataIndex: "userId",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    title: "开始时间",
    slotName: "startTime",
  },
  {
    slotName: "optional",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const router = useRouter();

/**
 * 跳转到考试页面
 * @param exam
 */
const toExamQuestion = (exam: Exam) => {
  router.push({
    path: "/view/examination",
    query: { id: exam.id },
  });
};

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  // 这里需要重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
};

const isExamAvailable = (startTime: string) => {
  if (!startTime) return false;
  return new Date().getTime() >= new Date(startTime).getTime();
};
</script>

<style scoped>
#examinationView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
